package com.beidouin.GnssMain.models;

/**
 * Created by flyingfluff on 16/9/21.
 */

public class PointList {

    private static PointList sInstance = new PointList();

    public static final int pointNumMax = 1000;
    private double[][] pointData = new double[pointNumMax][2];
//    private double[][] diffXY = new double[pointNumMax][2];
//    private double[] distance = new double[pointNumMax];
    private int pointNum = 0;
    private int pointCur = 0;
    private double[] pointSum = new double[2];
    private double[] pointAve = new double[2];
    private static final double earthRadius = 6378140.0;
    private boolean isUpdateAccuracy = false;
    //private double accuracy = 0;

    public static PointList getInstance() {
        return sInstance;
    }


    public void setIsUpdateAccuracy(boolean value) {isUpdateAccuracy = value; }

    public boolean getIsUpdateAccuracy() { return isUpdateAccuracy; }
    public int getPointNum() {return pointNum; }

    public int getListLength() {
        //full list
        int ret = pointNumMax;
        if (pointNum <= pointNumMax) {  //not full
            ret = pointNum;
        }
        return ret;
    }

    public int getCurrentPos() {
        //full list
        int ret = (pointCur - 1) % pointNumMax;
        if (pointNum < pointNumMax) {  //not full
            ret = pointNum - 1;
        }
        return ret;
    }

    public double[] getDiffXY(int index) {
        double[] diff = new double[2];

        double diffLat = (pointAve[0] - pointData[index][0]) * Math.PI / 180;
        double diffLon = (pointData[index][1] - pointAve[1]) * Math.PI / 180;
        diff[0] = earthRadius * Math.cos(pointAve[0] * Math.PI / 180) * diffLon;
        diff[1] = earthRadius * diffLat;

        return diff;
    }

    public void AddPoint() {
        if (pointNum == pointNumMax) {
            //delete the old point
            pointSum[0] -= pointData[pointCur][0];
            pointSum[1] -= pointData[pointCur][1];
            //get a new point
            String str = PilotInfo.getInstance().transLat(0, "latitude");
            str = str.substring(0, str.length()-1);
            double value = Double.parseDouble(str);
            if (PilotInfo.getInstance().getLatitudeUnit() == "S")
                value = -value;
            pointData[pointCur][0] = value;
            str = PilotInfo.getInstance().transLat(0, "longitude");
            str = str.substring(0, str.length()-1);
            value = Double.parseDouble(str);
            if (PilotInfo.getInstance().getLongitudeUnit() == "W")
                value = -value;
            pointData[pointCur][1] = value;
            //add new point
            pointSum[0] += pointData[pointCur][0];
            pointSum[1] += pointData[pointCur][1];
            //update pointer
            pointCur = (pointCur + 1) % pointNumMax;
        } else {
            //get a new point
            String str = PilotInfo.getInstance().transLat(0, "latitude");
            str = str.substring(0, str.length()-1);
            double value = Double.parseDouble(str);
            pointData[pointNum][0] = value;
            str = PilotInfo.getInstance().transLat(0, "longitude");
            str = str.substring(0, str.length()-1);
            value = Double.parseDouble(str);
            pointData[pointNum][1] = value;
            //add a new point
            pointSum[0] += pointData[pointNum][0];
            pointSum[1] += pointData[pointNum][1];
            //update number
            ++pointNum;
        }

        pointAve[0] = pointSum[0] / pointNum;
        pointAve[1] = pointSum[1] / pointNum;
    }

    public void ClearData() {
        pointNum = 0;
        pointCur = 0;
        pointSum = new double[2];
        pointAve = new double[2];
        //accuracy = 0;
    }
}
